package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.designations.DesignationsTransfer;
import com.admin.education.dataTransferObject.designations.request.CreateDesignationsRequest;
import com.admin.education.dataTransferObject.designations.request.UpdateDesignationsRequest;
import com.admin.education.services.Interfaces.IDesignationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@JwtAuthorize
@RestController
@RequestMapping("designations")
@CrossOrigin(origins = "*", maxAge = 3600)
public class DesignationsController {
    @Autowired
    private IDesignationsService designationsService;
    @GetMapping("")
    public Callable<IResponseMessage> queryDesignations(@Valid PageRequest pageRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<PageResponse<DesignationsTransfer>> servicesExcuteResult = designationsService.queryDesignationss(pageRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<DesignationsTransfer>>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return  responseMessage;
            }
        };
    }
    @PostMapping("")
    public Callable<IResponseMessage> createDesignations(@RequestBody @Valid CreateDesignationsRequest createDesignationsRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = designationsService.createDesignations(createDesignationsRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PutMapping("{designationsId}")
    public Callable<IResponseMessage> updateDesignations(@PathVariable Long designationsId,@RequestBody @Valid UpdateDesignationsRequest updateDesignationsRequest){
        return  new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = designationsService.updateDesignations(designationsId,updateDesignationsRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PutMapping("{designationsId}/valid")
    public Callable<IResponseMessage> validDesignations(@PathVariable Long  designationsId)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = designationsService.setValid(designationsId);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{id}")
    public Callable<IResponseMessage> queryDesignationsById(@PathVariable Long id)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<DesignationsTransfer> servicesExcuteResult = designationsService.queryDesignationsById(id);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<DesignationsTransfer>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @DeleteMapping("{id}")
    public  Callable<IResponseMessage> deleteDesignationsById(@PathVariable Long id){
        return  ()->{
            IResponseMessage responseMessage = null;
            ServicesExcuteResult servicesExcuteResult = designationsService.deleteDesignations(id);
            if(servicesExcuteResult.isSuccess()){
                responseMessage = new ResponseMessage(Result.Success);
            }
            else{
                throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
            }
            return responseMessage;
        };
    }
}
